'use strict'

module.exports = (app) => {
  const { INTEGER, STRING, DATE, BOOLEAN } = app.Sequelize
  // 配置（重要：一定要配置详细，一定要！！！）
  const Permissions = app.model.define('permissions', {
    id: {
      type: INTEGER(20).UNSIGNED,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: STRING(30),
      comment: '权限名称'
    },
    class: {
      type: STRING,
      comment: '分组类型'
    },
    class_name: {
      type: STRING,
      comment: '分组名称'
    },
    is_parent: {
      type: BOOLEAN,
      comment: 'true:父级,false:子级'
    },
    url: {
      type: STRING,
      comment: '请求的路径'
    },
    created_time: {
      type: DATE,
      get() {
        return app.formatTime(this.getDataValue('created_time'))
      }
    }
  })
  // 关联关系
  Permissions.associate = (models) => {
    // 多对多 角色
    Permissions.belongsToMany(app.model.Role, {
      through: app.model.RolePermissions,
      foreignKey: 'pid',
      otherKey: 'rid'
    })
  }

  return Permissions
}
